package com.wwwarehouse.contract.convert_to_pc;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.GetImportToolIndexBean;
import com.wwwarehouse.common.common.Common;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.eventbus_event.PushImportEvent;
import com.wwwarehouse.common.eventbus_event.StartImportEvent;
import com.wwwarehouse.common.tools.ApkTools;
import com.wwwarehouse.contract.bean.PushImportBean;
import com.wwwarehouse.contract.bean.ShortUrlBean;
import com.wwwarehouse.contract.core.ContractConstant;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/9/7.
 * 带着分类信息的导入页面
 */
@Route(path = "/contract/ImportHaveSortFragment")
public class ImportHaveSortFragment extends BaseFragment implements View.OnClickListener {
	private String mUrl;
	private ShortUrlBean mShortUrlBean;
	private TextView mWebAddressTv;
	private long mOperationUkid;
	GetImportToolIndexBean bean;
	private ImageView mWechatIv;
	private String mTitle;

	@Override
	public void setTitle() {
		if (peekFragment() instanceof ImportHaveSortFragment) {
			/**
			 * IMPORT_TOOL 工具
			 IMPORT_GOODS 商品
			 IMPORT_RS_STORE  仓储
			 IMPORT_RS_LABOUR  劳务
			 IMPORT_RS_SALE  供货
			 IMPORT_RS_TRANSPORT  运输
			 */
			if (!TextUtils.isEmpty(mTitle) && !TextUtils.isEmpty(getZhString(mTitle))) {
				mActivity.setTitle(getZhString(mTitle));
			} else {
				mActivity.setTitle(R.string.import_with_sort_title);
			}
		}
	}

	private String getZhString(String eng) {
		if (eng.equals("IMPORT_TOOL")) {
			return "导入设备信息";
		}
		if (eng.equals("IMPORT_GOODS")) {
			return "导入商品信息";
		}
		if (eng.equals("IMPORT_RS_STORE")) {
			return "导入仓储信息";
		}
		if (eng.equals("IMPORT_RS_LABOUR")) {
			return "导入劳务信息";
		}
		if (eng.equals("IMPORT_RS_SALE")) {
			return "导入供货信息";
		}
		if (eng.equals("IMPORT_RS_TRANSPORT")) {
			return "导入运输信息";
		}
		return "";
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.bulk_import_has_sort_fragment_layout, container, false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		EventBus.getDefault().register(this);
		mWebAddressTv = (TextView) view.findViewById(R.id.web_address_tv);
		mWechatIv = (ImageView) view.findViewById(R.id.share_wechat_iv);
		view.findViewById(R.id.share_qq_iv).setOnClickListener(this);
		mWechatIv.setOnClickListener(this);
		if (!ApkTools.isWeixinInstall(mActivity)) {
			mWechatIv.setVisibility(View.GONE);
		}
		if (getArguments() != null && getArguments().getSerializable(Constant.KEY_INTENT_IMPORT_TOOL) != null) {
			 bean = (GetImportToolIndexBean) getArguments().getSerializable(Constant.KEY_INTENT_IMPORT_TOOL);
			if (bean != null && bean.getUrl() != null) {
				String url = bean.getUrl();
				mUrl = url;
				mWebAddressTv.setText("操作网址：" +mUrl);
//				mOperationUkid = bean.getOperationUkid();
			}
		}
		if (getArguments() != null && getArguments().getString(Constant.KEY_INTENT_IMPORT_TYPE) != null) {
			mTitle = getArguments().getString(Constant.KEY_INTENT_IMPORT_TYPE);
		}


	}

	@Override
	public void requestDatas() {

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.share_qq_iv) {
			if (mUrl != null ) {
				Common.getInstance().shareQQ(mActivity , mUrl,"" , "" ,mUrl );
			}
		} else if (v.getId() == R.id.share_wechat_iv) {
			if (mUrl!= null ) {
				Common.getInstance().shareWeixinTextMoment(mActivity, mUrl);
			}
		}

	}

	/**
	 * 接收推送消息，跳转到导入成功页面。
	 * @param pushImportEvent
	 */
	public void onEventMainThread(PushImportEvent pushImportEvent){
		if (peekFragment() instanceof ImportHaveSortFragment) {
			PushImportBean pushImportBean = JSON.parseObject(pushImportEvent.getResult(), PushImportBean.class);
			if (pushImportBean != null && pushImportBean.getMsg() != null ) {
				Bundle bundle = new Bundle();
				bundle.putLong(Constant.KEY_INTENT_IMPORT_TOOL_PUSH_CLICK, Long.valueOf(pushImportBean.getMsg().getPushImportCount()));
				ImportSuccessFragment importSuccessFragment = new ImportSuccessFragment();
				importSuccessFragment.setArguments(bundle);

				//能不能先pop再push，要不然，push有何用？
				popFragment();

				pushFragment(importSuccessFragment);
			}
		}
	}

	/**
	 * 接收推送消息，跳转到计时页面。
	 * @param startImportEvent
	 */
	public void onEventMainThread(StartImportEvent startImportEvent){
		if (peekFragment() instanceof ImportHaveSortFragment) {
			PushImportBean mStartImportBean = JSON.parseObject(startImportEvent.getResult(), PushImportBean.class);
			if (mStartImportBean != null && mStartImportBean.getMsg() != null ) {
				Bundle bundle = new Bundle();
				if (!TextUtils.isEmpty(mStartImportBean.getMsg().getCostTime())) {
					bundle.putLong(ContractConstant.KEY_BUNDLE_PUSH_TO_DUTATIONTIME, Long.valueOf(mStartImportBean.getMsg().getCostTime()));
				}
				bundle.putString(Constant.KEY_PC_IMPORT_UNIFY_SHORT_URL , mUrl);
				bundle.putString(Constant.KEY_INTENT_IMPORT_TYPE, mTitle);
				DurationTimeFragment durationTimeFragment = new DurationTimeFragment();
				durationTimeFragment.setArguments(bundle);

				popFragment();

				pushFragment(durationTimeFragment);
			}
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

}
